﻿using AutoMapper;
using BasicData.Application.Input;
using BasicData.Application.Input.Product;
using BasicData.Application.Output;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using BasicData.Result;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BasicData.Application.Services.Product
{
    public class ProductServices : IProductServices
    {
        IBasicModelRepository<ProductModel> _repository;
        IBasicModelRepository<MaterialModel> _materialRepository;
        IMapper mapper;

        public ProductServices(IBasicModelRepository<ProductModel> repository, IBasicModelRepository<MaterialModel> materialRepository, IMapper mapper)
        {
            _repository = repository;
            _materialRepository = materialRepository;
            this.mapper = mapper;
        }

        public ApiResult<int> AddProduct(ProductInput input)
        {
            var product = mapper.Map<ProductModel>(input);
            product.Id = YitIdHelper.NextId();
            product.CreatedAt = DateTime.Now;
            product.CreatedBy = "wyj";
            _repository.Create(product);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "添加成功",
                Data = 1
            };
        }

        public ApiResult<int> DeleteProduct(long id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _repository.GetValue().FirstOrDefault(x => x.Id == id);
            if (model == null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "未找到该数据";
                return res;
            }
            model.IsDeleted = true;
            int i = _repository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Error;
                res.Message = "删除失败";
                res.Data = i;
            }
            return res;
        }

        public ApiPaging<ProductOutput> GetProduct(PageInput input)
        {
            ApiPaging<ProductOutput> res = new ApiPaging<ProductOutput>();
            var products = _repository.GetValue().Where(x => x.IsDeleted == false);
            var material = _materialRepository.GetValue().Where(x => x.IsDeleted == false);
            var query = from a in products
                        join b in material on a.MaterialId equals b.Id
                        select new ProductOutput()
                        {
                            Id = a.Id,
                            ProductCode = a.ProductCode,
                            ProductName = a.ProductName,
                            Specification = a.Specification,
                            Unit = a.Unit,
                            Warning = a.Warning,
                            MaterialId = a.Id,
                            MaterialName = b.MaterialName,
                            MaterialType = a.MaterialType,
                            ProductMsg = a.ProductMsg,

                        };
            res.TotalCount = products.Count();
            res.PageDatas = query.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            return res;
        }

        public ApiResult<int> UpdateProduct(UpdateProductInput input)
        {
            var modedl = mapper.Map<ProductModel>(input);
            modedl.UpdatedAt = DateTime.Now;
            modedl.CreatedBy = "wyj";
            _repository.Update(modedl);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "修改成功",

            };
        }
    }
}
